package adminServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.print.attribute.Size2DSyntax;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ticketEntity.Ticket;
import ticketImplment.TicketImplment;
import userEntity.User;
import userImplment.UserImplement;

/**
 * Servlet implementation class AdminQueryUser
 */
public class AdminQueryUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminQueryUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String Uname = request.getParameter("Uname");
		String Rname = request.getParameter("Rname");
		HttpSession session = request.getSession();
		UserImplement userImplement=new UserImplement();
		List<User> userList=new ArrayList<User>();
		if((Uname.equals("按用户名查询"))&&(Rname.equals("按车次名查询购票用户"))) {
			
			session.setAttribute("userList",userImplement.GetAllUser());
			request.getRequestDispatcher("adminUser.jsp").forward(request, response);
		}else if(!Uname.equals("按用户名查询")){
			if(userImplement.Query(Uname)!=null) {
			User user=userImplement.Query(Uname);
			userList.add(user);
			session.setAttribute("userList", userList);
			}
			request.getRequestDispatcher("adminUser.jsp").forward(request, response);
		}else if(!Rname.equals("按车次名查询购票用户")) {
			TicketImplment ticketImplment=new TicketImplment();
			if(ticketImplment.QueryTicketRname(Rname)!=null) {
				List<Ticket> list=ticketImplment.QueryTicketRname(Rname);//指定车次卖出的全部的票
				for(int i=0;i<list.size();i++) {	
					if(userImplement.Query(list.get(i).getUname())!=null){
					userList.add(userImplement.Query(list.get(i).getUname()));
					}//通过车票查询用户名，再通过用户名查询到用户,再添加到userList中
				}
				session.setAttribute("userList", userList);
			}
			request.getRequestDispatcher("adminUser.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("adminUser.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
